<?php
/**
 *   Joomdiet - components/models/recipe.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class JoomdietModelRecipe extends JModel
{
    var $_data;
    var $_query;
    
    function __construct()
    {
        parent::__construct();
    }
    
    function _buildQuery()
    {
        $this->_query = 'SELECT r.* FROM #__joomdiet_Recipes as r';
        $this->_query .= ' LEFT JOIN #__joomdiet_Recipes_Category as rc ON r.recipe_category_id = rc.id';
        $this->_query .= ' LEFT JOIN #__joomdiet_Recipe_food as rf ON r.id = rf.id_recipe';
        $this->_query .= ' LEFT JOIN #__joomdiet_Food_variable as fv ON rf.id_food = fv.id_food';
        return $this->_query;
    }
    
    function _buildWhereQuery($filter='', $category='', $subcategory=0, $composition=0)
    {
        $this->_query .= ' WHERE 1=1';
        
        // text filter
        if ($filter != '') {
            $filter_words = explode(' ', $filter);
            foreach ($filter_words as $word) {
                $this->_query .= ' AND r.name LIKE "%'.$word.'%"';
            }
        }
        
        // category filter
        if ($category != 0) {
            $this->_query .= ' AND r.recipe_category_id = '.$category;
        }
        
        // subcategory filter
        if (($category != 0) and ($subcategory != 0)) {
            $this->_query .= ' AND r.recipe_category_id = '.$subcategory;
        }
        
        // variable filter
        if ($composition = 0) {
            $this->_query .= ' AND fv.id_variable';
        }
        
        return $this->_query;
    }
    
    function _buildEndQuery() 
    {
        $this->_query .= ' GROUP BY r.id';
    }
    
    function getData($filter='', $category=0, $subcategory=0, $composition=0)
    {
        $this->_buildQuery();
        $this->_buildWhereQuery($filter, $category, $subcategory, $composition);
        $this->_buildEndQuery();
        
        if (empty($this->_data)) {
            $this->_data = $this->_getList($this->_query);
        }
        
        return $this->_data;
    }
    
    function getMixedData($filter='')
    {
        $query = 'select * from (SELECT *,0+1 type FROM #__joomdiet_Recipes WHERE name LIKE "%'.$filter.'%"UNION SELECT *,0+2 type FROM #__joomdiet_Food WHERE name LIKE "%'.$filter.'%") a order by a.name';
        $this->_db->setQuery($query);
        
        if (empty($this->_data)) {
            $this->_data = $this->_db->loadObjectList();
        }

        return $this->_data;
    }
    
    function getRecipe($recipe_id)
    {
        $query = 'SELECT * FROM #__joomdiet_Recipes';
        $query .= ' WHERE id='.$recipe_id;
        $this->_db->setQuery($query);
        return $this->_db->loadObject();
    }
}
?>
